package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.management.service.CourseService;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @文件名: CourseController
 * @作者: DX
 * @创建日期: 2025/10/8
 * @描述:
 */
@Slf4j
@Builder
@RestController
@RequestMapping("/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping("/subject/{subject}")
    public Result coursesBySubject(@PathVariable Integer subject){
        log.info("学科查询课程：{}", subject);
        List<Course> list = courseService.coursesBySubject(subject);
        return Result.success(list);
    }

    @PutMapping
    public Result update(@RequestBody Course course){
        log.info("更新课程：{}", course);
        courseService.update(course);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id){
        log.info("查询课程：{}", id);
        Course course = courseService.getById(id);
        return Result.success(course);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除课程：{}", id);
        courseService.deleteById(id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Course course){
        log.info("添加课程：{}", course);
        courseService.add(course);
        return Result.success();
    }


    @GetMapping("/list")
    public Result list() {
        List<Course> listAll = courseService.listAll();
        log.info("查询课程列表：{}", listAll);
        return Result.success(listAll);

    }

    @GetMapping
    public Result list(
           @RequestParam(name = "name", required = false)  String  name,
           @RequestParam(name = "subject", required = false) Integer subject,
           @RequestParam(name = "target", required = false) Integer target ,
           @RequestParam(name = "page", defaultValue = "1") Integer page ,
           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        log.info("查询课程列表：{},{},{},{},{}", name , subject, target, page, pageSize);
        PageResult<Course> pageResult = courseService.pageResult( name, subject, target, page, pageSize);
        return Result.success(pageResult);
    }

}
